package middleware

import (
	"api-gateway/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AuthToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if token == "" {
			c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "token不能为空"})
			c.Abort()
			return
		}
		userId, err := pkg.ParseJwtToken(token)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "token解析失败"})
			c.Abort()
			return
		}
		c.Set("userId", userId)
		c.Next()
	}
}
